import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String a;
        while (true) {
            int[] test = generateAnswer();
            while (true) {
                int b = compareGuessAnswer(test, getPlayerInput());
                if (b == 4) {
                    System.out.println("您猜对了！您获胜了");
                    break;
                }else {
                    System.out.println("您猜错了，请重新输入");
                }
            }
            System.out.println("按Y继续游戏，按N退出游戏");
            while (true){
                a = scanner.next();
                if (a.equals("y")||a.equals("n")){
                    break;
                }else {
                    System.out.println("请输入Y/N");
                }
            }
            if (a.equals("y")) {
                continue;
            } else {
                break;
            }
        }
    }


    public static int[] generateAnswer() {
        Random random = new Random();
        int[] gpr = new int[4];
        int c = random.nextInt(10);
        gpr[0] = c;
        for (int i = 0; i < gpr.length; i++) {
            c = random.nextInt(10);
            for (int num = 0; num < i; num++) {
                while (c == gpr[num]) {
                    c = random.nextInt(10);
                    num = 0;
                }
            }
            gpr[i] = c;
        }
        for (int i = 0; i < gpr.length; i++) {
            for (int j = i + 1; j < gpr.length; j++) {
                if (gpr[i] == gpr[j]) {
                    gpr[j] = (gpr[j] * gpr[j] + 1) % 10;
                }
            }
        }
        System.out.println(Arrays.toString(gpr));

        return gpr;
    }


    public static int[] getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        int[] num = new int[4];
        System.out.println("请输入四个不同的数字");
        for (int i = 0; i < num.length; i++) {
            System.out.println("第"+(i + 1)+"个数");
            num[i] = scanner.nextInt();
        }
        return num;
    }


    public static int compareGuessAnswer(int[] guess, int[] number) {
        int a = 0, b = 0;
        for (int i = 0; i < guess.length; i++) {
            for (int k : number) {
                if (guess[i] == number[i]) {
                    a++;
                    break;
                }
                if (k == guess[i]) {
                    b++;
                }
            }
        }
        return a;
    }


    public static boolean isWin(int a) {
        boolean p;
        if (a == 4) {
            p = true;
        } else {
            p = false;
        }
        return p;
    }
}
